package cn.schoolwow.flow.monitor.module.business.scheduleJob.definition.aware;

import cn.schoolwow.ams.domain.block.common.operation.OpenFieldTabBlockOperation;
import cn.schoolwow.ams.domain.block.list.common.ListBlockButtonType;
import cn.schoolwow.ams.domain.block.list.dao.crud.DatabaseListBlockListListener;
import cn.schoolwow.ams.domain.block.list.dao.crud.DatabaseListBlockTableField;
import cn.schoolwow.ams.domain.block.list.dao.entityTable.EntityTableListBlockConfigAware;
import cn.schoolwow.ams.domain.block.list.dao.entityTable.EntityTableListBlockOption;
import cn.schoolwow.flow.monitor.domain.FlowMonitorOption;
import cn.schoolwow.flow.monitor.entity.ScheduleJobLog;
import cn.schoolwow.flow.monitor.module.business.scheduleJob.definition.flow.business.GetScheduleJobContextListFlow;
import cn.schoolwow.flow.monitor.module.common.domain.FlowMonitorConstant;
import cn.schoolwow.flow.monitor.module.common.flow.AddFlowCategoryNameCascadeRemoteSelectButton;
import cn.schoolwow.flow.monitor.module.common.flow.AddFlowNameRemoteSelectButton;
import cn.schoolwow.quickdao.domain.database.dql.condition.Condition;
import cn.schoolwow.quickdao.domain.database.dql.response.PageVo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

@Service
public class ScheduleJobLogAware extends EntityTableListBlockConfigAware {
    @Autowired
    private FlowMonitorOption flowMonitorOption;

    @Override
    public EntityTableListBlockOption getAMSBlockConfig() {
        EntityTableListBlockOption entityTableListBlockOption = new EntityTableListBlockOption(
                FlowMonitorConstant.PROJECT_NAME, "定时任务日志", FlowMonitorConstant.DAO_NAME,
                ScheduleJobLog.class);
        entityTableListBlockOption.listBlockShowOption.displayFieldNames = new String[]{
                "name","startTime","endTime", "consumeTime","status"
        };
        entityTableListBlockOption.listBlockShowOption.requestFieldNames = new String[]{
                "name","status"
        };
        entityTableListBlockOption.listBlockShowOption.listBlockButtonTypeArray = new ListBlockButtonType[]{
                ListBlockButtonType.DELETE,
                ListBlockButtonType.MULTIPLE_DELETE,
                ListBlockButtonType.CONDITION_DELETE,
                ListBlockButtonType.ALL_DELETE,
        };

        OpenFieldTabBlockOperation openFieldTabBlockOperation = new OpenFieldTabBlockOperation("查看详情",
                Arrays.asList("flowTrace", "sql", "exception", "requestData", "contextData", "quickFlowData", "flowConfigData"));
        entityTableListBlockOption.blockOperationList.add(openFieldTabBlockOperation);

        setListListener(entityTableListBlockOption);
        flowMonitorOption.flowRecordQuickFlow.startFlow(new GetScheduleJobContextListFlow())
                .next(new AddFlowCategoryNameCascadeRemoteSelectButton())
                .next(new AddFlowNameRemoteSelectButton())
                .putInstanceData(entityTableListBlockOption)
                .execute();
        return entityTableListBlockOption;
    }

    private void setListListener(EntityTableListBlockOption entityTableListBlockOption){
        entityTableListBlockOption.databaseListBlockListener.listListener = new DatabaseListBlockListListener() {
            @Override
            public void beforeExecuteCondition(Condition condition, JSONObject queryCondition) throws Exception {
                condition.orderByDesc("createdAt");
                if(queryCondition.containsKey("categoryName")){
                    String categoryName = queryCondition.getString("categoryName");
                    categoryName = categoryName.replace(",","-");
                    queryCondition.put("categoryName", categoryName);
                }
                Integer logType = queryCondition.getInteger("logType");
                if(null!=logType){
                    switch (logType){
                        case 1:{
                            condition.addNullQuery("brokenReason")
                                    .addNullQuery("exception");
                        }break;
                        case 2:{
                            condition.addNotNullQuery("exception");
                        }break;
                        case 3:{
                            condition.addNotNullQuery("brokenReason");
                        }break;
                    }
                }
            }

            @Override
            public void afterExecuteCondition(Condition condition, JSONObject queryCondition, PageVo pageVo) throws Exception {

            }

            @Override
            public DatabaseListBlockTableField[] listTableFieldArray() {
                return new DatabaseListBlockTableField[]{
                        new DatabaseListBlockTableField("logType", "日志类型(0:全部,1:正常日志,2:异常日志,3:中断日志)", "select", true)
                };
            }
        };
    }
}
